
import {getForexList, getForexById,  addForex, exchange, deleteForex, approvalForex} from '../../services/foreignExchange/foreignExchangeManage';
import {getClaimForexList} from '../../services/foreignExchange/chaim';
import {parse} from 'qs';
import { message } from 'antd';

const dateFormat = 'YYYY-MM-DD';

export default {
    namespace: 'foreignExchangeManage',
    state: {
        search: { // 查询条件
            payman: "",
            beginAmt: "",
            endAmt: "",
            payaccount: "",
            rectime: "",
            endTime: "",
            beginTime: ""
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            defaultWidth: 800, // 缺省宽度800
            width: 800, // 宽度
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '创建外汇', // 标题
            step: 1, // 步骤
            page: {
                page: 1,
                size: 10,
                totalRow: 0,
                totalPage: 0
            },
            list: [],
        }
    },
    reducers: {
        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return {...state, search: action.payload.search};
        },


        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = {...state.editModel};
            if(payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if(payload.width != undefined) {
                newState.width = payload.width;
            }
            if(payload.title != undefined) {
                newState.title = payload.title;
            }
            if(payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if(payload.step != undefined) {
                newState.step = payload.step;
            }

            if(payload.customsMode != undefined) {
                newState.customsMode = payload.customsMode;
            }
            if(payload.cencasementMode != undefined) {
                newState.cencasementMode = payload.cencasementMode;
            }
            if(payload.wordSendMode != undefined) {
                newState.wordSendMode = payload.wordSendMode;
            }

            if(payload.type != undefined) {
                newState.type = payload.type;
            }
            if(payload.form != undefined) {
                newState.form = payload.form;
            }
            if(payload.page != undefined) {
                newState.page = payload.page;
            }
            if(payload.list != undefined) {
                newState.list = payload.list;
            }
            return {...state, editModel: {...newState}};
        },


        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return {...state, list: payload.list, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return {...state, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setEditPage(state, { payload }) {
            let newState = {...state.editModel};
            newState.page = payload.page;
            return {...state, editModel: {...newState}};
        }

    },
    effects: {

        *getForexList({}, { call, put, select }) {
            const search = yield select(state => state.foreignExchangeManage.search);
            const page = yield select(state => state.foreignExchangeManage.page);

            if (search.rectime != null && search.rectime != undefined) {
                if(search.rectime.length >=2 ){
                    if(search.rectime[0] != ''){
                        search.beginTime = search.rectime[0].format(dateFormat);
                    }
                    if(search.rectime[1] != ''){
                        search.endTime = search.rectime[1].format(dateFormat);
                    }

                }
            }

            const res = yield call(getForexList, {
                payman: search.payman,
                beginAmt: search.beginAmt,
                endAmt: search.endAmt,
                beginTime: search.beginTime,
                endTime: search.endTime,
                status: search.status,
                contractno: search.contractno,
                payaccount: search.payaccount,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page == undefined ? page : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        *getForexById({ payload }, { call, put, select }) {
            const res = yield call(getForexById, { id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                let type = "setEditModel";

                if (payload.step == 2) {
                    type = "getClaimForexList";
                }

                yield put({
                    type: type,
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "外汇结汇",
                        step: payload.step,
                        type: 'update',
                        form: {
                            id:  content.id,
                            recamount:  content.recamount,
                            rectime:  content.rectime,
                            payman:  content.payman,
                            payaccount:  content.payaccount,
                            settlement:  content.settlement,
                            recaccount:  content.recaccount,
                            memo:  content.memo,
                            exchangetime:  content.exchangetime,
                            bankexchangerate:  content.bankexchangerate,
                            bankexchangeamount:  content.bankexchangeamount,
                            cstexchangerate:  content.cstexchangerate,
                            cstexchangeamount:  content.cstexchangeamount,
                            type: payload.type,

                        }
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        *deleteForexById({payload}, {call, put, select}) {
            const res = yield call(deleteForex, {id : payload.id});
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                yield put({
                    type: "getForexList"
                });
            } else {
                message.error(res.data.errMessage);
            }

        },


        /**
         * 结汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *exchange({ payload }, { call, put, select }) {
            const res = yield call(exchange, payload);

            if (res.data.resultCode === "ok") {
                message.info("结汇成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getForexList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 新增外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addForex({payload}, {call, put, select}) {

            payload.rectime = payload.rectime.format(dateFormat);

            const res = yield call(addForex, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getForexList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取认领记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getClaimForexList({payload}, { call, put, select }) {

            const page = yield select(state => state.foreignExchangeManage.editModel.page);

            const res = yield call(getClaimForexList, {
                forexId: payload.form.id,
                page

            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: payload.showStatus,
                        confirmLoading: payload.confirmLoading,
                        title: payload.title,
                        step: payload.step,
                        type: payload.type,
                        form: {
                            id: payload.form.id,
                            recamount:  payload.form.recamount,
                            rectime:  payload.form.rectime,
                            payman:  payload.form.payman,
                            payaccount:  payload.form.payaccount,
                            settlement:  payload.form.settlement,
                            recaccount:  payload.form.recaccount,
                            memo:  payload.form.memo,
                            exchangetime:  payload.form.exchangetime,
                            bankexchangerate:  payload.form.bankexchangerate,
                            bankexchangeamount:  payload.form.bankexchangeamount,
                            cstexchangerate:  payload.form.cstexchangerate,
                            cstexchangeamount:  payload.form.cstexchangeamount,
                            type: payload.type,

                            // list: content.list,
                            // page: content.page == undefined ? payload.form.page : content.page,
                        },
                        list : content.list,
                        page: content.page == undefined ? payload.form.page : content.page,
                    }
                });

            }
        },


        /**
         * 外汇审核
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *approvalForex({ payload }, { call, put, select }) {
            const res = yield call(approvalForex, payload);
            if (res.data.resultCode === "ok") {
                if(payload.onSuccess)payload.onSuccess();
                message.info("审核成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getForexList'
                });
            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },


    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                dispatch({
                    type: 'getForexList'
                });
            });
        }
    }
};
